package com.caleble.sample.scheduling;

import org.apache.log4j.Logger;
import org.codehaus.xfire.XFire;
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;

public class SmsServiceImplXJ {
	Logger log = Logger.getRootLogger();

	private String serviceUrl = "";
	private String username = "";
	private String password = "";
	private String spid = ""; // 企业编号
	private String template = ""; // 短信内容模板

	public static final int WEBSERVICE_LOGIN = 1; // 登录
	public static final int WEBSERVICE_SENDMSG = 2; // 发送短信
	public static final int WEBSERVICE_GETREMAIN = 3; // 查询余量

	// Create a metadata of the service
	Service serviceModel = new ObjectServiceFactory().create(SmsServiceXJ.class);
	SmsServiceXJ client = null;

	// Create a proxy for the deployed service
	XFire xfire = XFireFactory.newInstance().getXFire();
	XFireProxyFactory factory = new XFireProxyFactory(xfire);

	public boolean callService(int service, String mobile, String content) {
		boolean successed = false;

		try {
			client = (SmsServiceXJ) factory.create(serviceModel, serviceUrl);
		} catch (Exception e) {
			log.error("短信发送平台WebService调用失败！(" + e.toString() + ")");
		}

		try {
			switch (service) {
			case SmsServiceImplXJ.WEBSERVICE_LOGIN:
				if ("1".equals(client.Login(username, password))) { // 远程调用
					successed = false;
					log.error("短信发送平台WebService调用失败！(登录失败)");
				}
				break;
			case SmsServiceImplXJ.WEBSERVICE_SENDMSG:
				if ("1".equals(client.SendMsg(username, password, spid, mobile, content))) { // 远程调用
					successed = false;
					log.error("短信发送平台WebService调用失败！(发送短信失败)");
				}
				break;
			case SmsServiceImplXJ.WEBSERVICE_GETREMAIN:
				if (0 == client.GetRemain(username, password, mobile)) { // 远程调用
					successed = false;
					log.error("短信发送平台WebService调用失败！(查询短信余量)");
				}
				break;
			default:
				break;
			}
		} catch (Exception e) {
			log.error("短信发送平台WebService调用失败！(" + e.toString() + ")");
		}

		return successed;
	}

	public String getSpid() {
		return spid;
	}

	public void setSpid(String spid) {
		this.spid = spid;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getServiceUrl() {
		return serviceUrl;
	}

	public void setServiceUrl(String serviceUrl) {
		this.serviceUrl = serviceUrl;
	}

	public String getTemplate() {
		return template;
	}

	public void setTemplate(String template) {
		this.template = template;
	}
}
